/*
 * =====================================================================================
 *
 *       Filename:  Socket.h
 *
 *    Description:  
 *
 *        Version:  1.0
 *        Created:  2012-10-01
 *       Revision:  none
 *       Compiler:  cmake & gcc
 *
 *         Author:  ankjt
 *
 * =====================================================================================
 */
#ifndef __SOCKET_H__
#define __SOCKET_H__

#include "SocketDefines.h"
#include "SendBuffer.h"
#include "RecvBuffer.h"


class SocketMgr;
class Socket
{
public:
    Socket(SOCKET sFd,int iSendBufferSize,int iRecvBufferSize);
    virtual ~Socket();

    void SetServerAddr(sockaddr_in& kAddr);
    SOCKET GetFd(){ return m_sSocket; }
    bool Connect(const char * pcAddress,int iPort);
    void DisConnect();

    void Accept(sockaddr_in * pkAddress);

    void OnRead(void* pData,int& iSize);

    bool Send(const uint8 * pBytes,int iSize);

    bool BurstSend(const uint8 * Bytes,int iSize);

    std::string GetRemoteIP();
    int GetRemotePort() { return ntohs(m_kClient.sin_port); }

    SendBuffer& GetSendBuffer(){ return  m_kSendBuffer; }
    RecvBuffer& GetRecvBuffer(){ return  m_kRecvBuffer; }

    void SetupReadEvent();
    bool ReadCallback(int iLen);
    bool WriteCallback();

    bool IsDeleted() { return m_bDeleted; }
    bool IsConnected() { return m_bConnected; }
    sockaddr_in & GetRemoteStruct() { return m_kClient; }

    in_addr GetRemoteAddress() { return m_kClient.sin_addr; }

protected:
    void _OnConnect();
  
    SocketMgr* m_pkMgr;

    SendBuffer m_kSendBuffer;
    RecvBuffer m_kRecvBuffer;

    bool m_bConnected;
    bool m_bDeleted;

    sockaddr_in m_kServer;
    sockaddr_in m_kClient;
    SOCKET      m_sSocket;
public:
    void PostEvent(int iEvents);
};


#endif
